<template>
  <div>
    <el-tabs v-model="levelType" @tab-click="handleClick">
      <el-tab-pane label="企业链接库" name="0"></el-tab-pane>
      <el-tab-pane label="我上传的链接" name="1" v-if="showTabs"></el-tab-pane>
    </el-tabs>
    <div class="card-list">
      <div v-for="item in tableList" :key="item.id" class="card-item">
        <div class="lookItem">
          <el-tag size="mini" @click="lookItem(item)">详情</el-tag>
        </div>
        <span class="check">
          <el-checkbox v-model="item.checked" @change="checkChange($event,item)"></el-checkbox>
        </span>
        <el-image :src="item.coverUrl" fit="contain" style="width: 140px; height: 160px"></el-image>
        <div class="text-info">
          <p class="title">{{ item.title }}</p>
          <p class="verify">{{ item.name }}</p>
          <p class="time">{{ item.createTime }}</p>
        </div>
      </div>
    </div>
    <div class="page">
      <NormalPagination :currentPage="pageNum" :pageSize="pageSize" :total="total" layout="prev, pager, next" @handleCurrentChange="handleCurrentChange"></NormalPagination>
    </div>
    <div class="btn-list">
      <el-button type="info" plain @click="selectCancelFn">取消</el-button>
      <el-button type="primary" @click="selectSureFn">确定</el-button>
    </div>
  </div>
</template>

<script>
import { getLinkmanage } from "@/api/article/group";

export default {
  props: {
    linkList: {
      type: Array,
      default: () => []
    },
    showTabs: {
      type: Boolean,
      default: true,
    }
  },
  data() {
    return {
      levelType: "0",
      pageNum: 1,
      pageSize: 5,
      total: 0,
      tableList: [],
      currentSelectList: [],
    };
  },
  mounted() {
    this.currentSelectList = JSON.parse(JSON.stringify(this.linkList))
    this.getList();
  },
  methods: {
    checkChange(e, item) {
      if (e) {
        this.currentSelectList.push({
          name: item.title,
          coverUrl: item.coverUrl,
          id: item.id,
          levelType: item.levelType,
          materialUrl: item.materialUrl,
          mediaType: item.mediaType,
          materialType: 11,
          materialName: item.title,
        })
      } else {
        this.currentSelectList.forEach((element, index) =>{
          if (item.mediaType == 11 && element.id == item.id)
            this.currentSelectList.splice(index, 1);
        })
      }
    },
    handleClick() {
      this.pageNum = 1;
      this.tableList = [];
      this.total = 0;
      this.getList();
    },
    getList() {
      getLinkmanage({
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        mediaType: 11,
        levelType: this.levelType,
      }).then((res) => {
        if (res.code === 200) {
          this.tableList = res.rows.map((item) => {
            return {
              ...item,
              checked: false,
              coverUrl: window.globalConfig.IMAGE_API + item.coverUrl,
            };
          });
          this.currentSelectList.length > 0 && this.tableList.forEach(element => {
            this.currentSelectList.forEach(item => {
              if (item.mediaType == 11 && element.id == item.id) element.checked = true;
            })
          })
          this.total = res.total;
        }
      });
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.getList();
    },
    selectCancelFn() {
      this.$emit("selectCancelFn");
    },
    selectSureFn() {
      this.$emit("confirm", this.currentSelectList);
    },
    lookItem(item) {
      this.$emit('view', item)
    },
  },
};
</script>


<style lang="scss" scoped>
.card-list {
  display: flex;
  flex-wrap: wrap;
  min-height: 264px;
  .card-item {
    position: relative;
    width: 152px;
    margin-right: 20px;
    margin-bottom: 20px;

    .check {
      position: absolute;
      top: 10px;
      right: 10px;
      z-index: 99;
    }

    &:nth-child(4n) {
      margin-right: 0;
    }

    &:hover {
      .lookItem {
        display: block;
      }
    }

    .lookItem {
      position: absolute;
      display: none;
      top: 30px;
      right: 10px;
      z-index: 999;

      .el-tag {
        display: block;
        width: 42px;
        margin: 5px 0;
        text-align: center;
        cursor: pointer;
      }
    }
  }
}

.page {
  text-align: right;
}

.btn-list {
  margin-top: 10px;
  text-align: right;
}

.text-info {
  display: flex;
  flex-direction: column;
  line-height: 2;

  .title {
    color: #606266;
  }

  .verify {
    text-align: right;
    color: #082aff;
  }

  .time {
    text-align: right;
    color: #999;
  }
}
</style>
